#!/usr/bin/env zsh

set -e

_DIR=$(cd "$(dirname "$0")"; pwd)
cd $_DIR


if [ -e "$1.json.gz" ]; then

#pigz -dc  $1.json.gz|jq -r '.eventParameters.userId+","+.deviceIdentifiers.AndroidId+","+.sessionTimestamp[:-3]+","+.firstSessionTimestamp[:-3]+","+.eventParameters."lang&mcc"' > $1.phone
pigz -dc  $1.json.gz|jq -r '.eventParameters.userId+","+.deviceIdentifiers.AndroidId+","+.sessionTimestamp[:-3]+","+.firstSessionTimestamp[:-3]+","+.eventParameters."lang&mcc"' | uniq > $1.phone
sort -r -n -t ','  -k 1,2 -u -o $1.phone $1.phone

(echo "phone,android_id,time,create_time,lang_mcc" && cat $1.phone) > $1.phone.csv
rm -rf $1.phone.db
rm -rf $1.phone.db.zst
rm  -rf $1.phone
(echo .mode csv;echo .import $1.phone.csv voip)|sqlite3 $1.phone.db
zstd -19 --rm $1.phone.db
scp $1.phone.db.zst r5:/root/zst/
fi
